<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/6/20
 * Time: 9:51
 */
namespace Admin\Controller;
use Think\Controller;

class MoneyDetailController extends CommonController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '财务管理';
        $this->breadcrumb2 = '平台月度营收管理';
        $this->sqlname='moneydetail'.session('user_auth')['uid'];
        $this->url = U('MoneyDetail/index');
    }

    //首页
    public function index(){
        $year=I('post.year');
        $month=I('post.month');

        $list=M('monthmoney');

        $this->updatemoney();
        if(is_numeric($year)){
            $where['sum_date']=array('like',"$year%");
            $list->where($where);
        }

        if(is_numeric($year) && is_numeric($month)){
            $date=$year.$month;
            $where['sum_date']=$date;
            $list->where($where);
        }

        $sql=$list->select(false);
        $redis = S(array('type' => 'redis'));
        S($this->sqlname, $sql);
        $sql=S($this->sqlname);
        $list=M('monthmoney')->where($where)->select();
        $this->assign('list',$list);
        $this->display();
    }


    //导出excel
    public function excelout(){
        // phpExcel();
        //exit;
        /*    $sheetindex=array(
                'A'=>array('index'=>'A1','name'=>'订单编号','size'=>'20'),
                'B'=>array('index'=>'B1','name'=>'故障金额','size'=>'20'),
                'C'=>array('index'=>'C1','name'=>'优惠券','size'=>'20'),
                'D'=>array('index'=>'D1','name'=>'诊断费','size'=>'20'),
                'E'=>array('index'=>'E1','name'=>'小费','size'=>'20'),
                'F'=>array('index'=>'F1','name'=>'订单总金额(元)','size'=>'20'),
                'G'=>array('index'=>'G1','name'=>'支付时间','size'=>'20'),
            );    */
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'年月','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'维修订单总金额','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'师傅佣金总金额','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'师傅奖励总金额','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'师傅签到总金额','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'师傅维修金','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'师傅维修保证金','size'=>'20'),
            'I'=>array('index'=>'I1','name'=>'维修保证金利息总金额','size'=>'20'),
        );
        $title='平台月度营收管理';
        $type=9;
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname);
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;

    }

    //更新月度资金
    private function updatemoney(){
      //  $now_month=date('m',time());
      //  $before_month=$now_month-1;
      //  $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月的开始日期
       // $t = date('t',strtotime($m)); //上个月共多少天

        $start = date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y'))); //当月的开始日期
        $date=str_replace('-','',$start);
        $date=substr_replace($date,'',6);

       // $end = date('Y-m-d', mktime(0,0,0,date('m')-1,$t,date('Y'))); //上个月的结束日期
        // mktime(0,0,0,$before_month)
        $start_time=strtotime($start);
        $end_time=time();
        $where['ro.pay_time']=array('between',array($start_time,$end_time));
        // 订单总金额 维修金 保证金 利息总金额
        $total=M('repair_order ro')
            ->join('left join zsf_repair_order_protocol p on p.order_id=ro.repair_order_num_alias')
            ->join('left join zsf_ratemanage r on r.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->field('sum(ro.total-ro.diagnosis_cost) total,sum(r.money) rate,sum(p.bond) bond,sum(p.maintain) maintain')
            ->find();
        $rewards=M('rewards_money')->where('money>0')->field('sum(money) money')->find();   //奖励
        $rewards_total=M('rewards_money')->field('sum(money) money')->find();        //佣金总金额

        $data['sum_date']=$date;
        $data['r_ordertotal']=$total['total'];
        $data['master_total']=$rewards_total['money'];
        $data['master_rewards']=$rewards['money'];
        $data['master_signin']=0;
        $data['master_repair']=$total['maintain'];
        $data['master_bond']=$total['bond'];
        $data['repair_interest']=$total['rate'];
        $data['add_time']=time();

        $zhewh['sum_date']=$date;
        $is_exist=M('monthmoney')->where($zhewh)->find();
        if($is_exist){
            $a=M('monthmoney')->where($zhewh)->save($data);
        }else{
            M('monthmoney')->add($data);
        }
    }
}